// pages/exam2/exam2.js
const app = getApp();
Page({
  data: {
    tabs: ['全部', '单选', '多选', '判断'],
    category: app.globalData.category,
    currentTab: 0,
    questions: [],
    safeTop: app.globalData.safearea.top
  },
  onLoad () {
    if (!app.globalData.token) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      setTimeout(() => {
        wx.switchTab({
          url: '/pages/index/index',
        });
      }, 1500);
      return;
    }
    if (!this.isLoad) {
      this.isLoad = true;
      this.loadData();
    }
  },
  onShow() {
    if (this.isLoad && this.data.category.id !== app.globalData.category.id) {
      this.setData({
        category: app.globalData.category
      });
      this.loadData();
    }
  },
  loadData(type_id = -1, data = {}) {
    wx.showLoading();
    data.category_id = this.data.category.id
    if (type_id > 0) {
      data.type_id = type_id;
    }
    const that = this;
    app.request('/api/question/list', data).then(res => {
      let questions = res.data.data ? res.data.data : [];
      for (let i = 0;i < questions.length;i++) {
        console.log('createtime', questions[i].createtime);
        questions[i].createtime_at = that.formatDate(questions[i].createtime);
        if (questions[i].question_type_id === 1) {
          questions[i].tag = '单选题';
        } else if (questions[i].question_type_id === 2) {
          questions[i].tag = '多选题';
        } else if (questions[i].question_type_id === 3) {
          questions[i].tag = '判断题';
        }
      }
      console.log(questions);
      that.setData({
        questions: questions
      });
      wx.hideLoading();
    }).catch(err => {
      wx.hideLoading();
      console.error(err);
    })
  },
  goSelect() {
    wx.navigateTo({
      url: '/pages/category/category?callback=/pages/exam2/exam2'
    });
  },
  goExam(e) {
    if (!('vip' in app.globalData.userinfo) || JSON.stringify(app.globalData.userinfo.vip) === '{}') {
      wx.showModal({
        title: '开通会员',
        content: '解锁本专业全部题目',
        confirmText: '确认开通',
        cancelText: '返回首页',
        complete: (res) => {
          if (res.cancel) {
            wx.switchTab({
              url: '/pages/index/index'
            });
          }
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/buy/buy'
            });
          }
        }
      })
      return;
    }
    app.globalData.exam1ParamsNeedLoad = true;
    // if (e.currentTarget.dataset.typeid === 1) {
    //   app.globalData.exam1Title = '单选题';
    // } else if (e.currentTarget.dataset.typeid === 2) {
    //   app.globalData.exam1Title = '多选题';
    // } else if (e.currentTarget.dataset.typeid === 3) {
    //   app.globalData.exam1Title = '判断题';
    // }
    app.globalData.exam1Title = '查看题目';
    app.globalData.exam1ViewMode = true;
    app.globalData.exam1ShowAnswer = true;
    app.globalData.exam1Params = {
      id: e.currentTarget.dataset.id
    };
    wx.navigateTo({
      url: '/pages/exam/exam',
    });
  },
  switchTab (e) {
    const index = e.currentTarget.dataset.index;
    if (index === this.data.currentTab) return;
    this.setData({
      currentTab: index
    });
    // 根据选中的标签筛选题目
    this.loadData(index);
  },
  bindblur(e) {
    if (!e.detail.value && !this.searchValue) return;
    this.loadData(this.data.currentTab, {title: e.detail.value});
    this.searchValue = e.detail.value;
  },
  onSearch() {
    this.bindblur({e: {detail: {value: this.searchValue}}});
  },
  
  formatDate(timestamp) {
    if(!timestamp){
      return '--'
    }
    var myDate = new Date(timestamp * 1000);
    var y = myDate.getFullYear();//年
    var m =(myDate.getMonth() + 1).toString().padStart(2, '0');//月
    var d =myDate.getDate().toString().padStart(2, '0');//日
    var hh =myDate.getHours().toString().padStart(2, '0') ;
    var mm =myDate.getMinutes().toString().padStart(2, '0') ;
    var ss = myDate.getSeconds().toString().padStart(2, '0');

    return [y, m, d].join('-') + ' ' + [hh, mm, ss].join(':');
  }
});